<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="icon" href="favicon.ico" />
        <title>后台登入</title>
        <link
            href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"
            rel="stylesheet"
        />
        <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="../css/wwl/login.css" />
    </head>
    <body>
        <div class="container">
            <div class="login-air">
                <form method="post">
                    <div class="form-group" id="user">
                        <label class="control-label" for="exampleInputUser1"
                            >用户名：</label
                        >
                        <input
                            type="text"
                            class="form-control"
                            id="inputUser1"
                            placeholder="用户名"
                            aria-describedby="help-block1"
                        />
                        <span id="help-block1" class="help-block"
                            >用户名不存在，请重新输入！</span
                        >
                    </div>
                    <div class="form-group" id="password">
                        <label class="control-label" for="exampleInputPassword1"
                            >密码：</label
                        >
                        <input
                            type="password"
                            class="form-control"
                            id="inputPassword1"
                            placeholder="Password"
                            aria-describedby="help-block2"
                        />
                        <span id="help-block2" class="help-block"
                            >密码不正确，请重新输入！</span
                        >
                    </div>
                    <div class="checkbox row">
                        <label class="col-md-5 control-label"
                            ><input
                                type="checkbox"
                                id="remember"
                            />记住我</label
                        >
                        <span class="col-md-2"> </span>
                        <a
                            href="./registered.html"
                            class="btn btn-primary col-md-5"
                            role="button"
                            >注册</a
                        >
                    </div>
                    <button
                        type="submit"
                        class="btn btn-default btn-block"
                        id="login"
                    >
                        登入
                    </button>
                </form>
            </div>
        </div>
        <script>
            let infoStr = localStorage.getItem("account");
            let infoStr2 = localStorage.getItem("info");
            let info1 = JSON.parse(infoStr);
            let info2 = JSON.parse(infoStr2);
            if (infoStr !== null) {
                $("#inputUser1").val(info1.user);
                $("#inputPassword1").val(info1.password);
            } else if (infoStr2 !== null) {
                $("#inputUser1").val(info2.user);
                $("#inputPassword1").val(info2.password);
            }
            $("#login").on("click", function (event) {
                event.preventDefault();
                const user = $("#inputUser1").val();
                const password = $("#inputPassword1").val();
                const userTips = $("#help-block1").css("display");
                const passwordTips = $("#help-block2").css("display");
                if (user === "" || infoStr === null || user !== info1.user) {
                    $("#user").addClass("has-error");
                    $("#help-block1").show();
                    return;
                } else {
                    $("#user").removeClass("has-error").addClass("has-success");
                    $("#help-block1").hide();
                    if (
                        password === "" ||
                        infoStr === null ||
                        password !== info1.password
                    ) {
                        $("#password").addClass("has-error");
                        $("#help-block2").show();
                        return;
                    } else {
                        $("#password")
                            .removeClass("has-error")
                            .addClass("has-success");
                        $("#help-block2").hide();
                    }
                }
                let rule =
                    (userTips === "none" && passwordTips === "none") ||
                    (user === "null" && password === "null");
                let checked = $("#remember").prop("checked");
                if (rule) {
                    if (checked) {
                        localStorage.setItem("info", JSON.stringify(info1));
                    } else {
                        localStorage.removeItem("info");
                    }
                    window.open("index.html");
                } else {
                    return;
                }
            });
        </script>
    </body>
</html>
